package com.stoa.floatlib.core

import android.app.Activity
import android.content.Context
import android.graphics.Point
import com.stoa.floatlib.MainIconStoaView
import com.stoa.floatlib.StoaFloatReal.lifecycleMap
import com.stoa.floatlib.constant.StoaFloatConstant
import com.stoa.floatlib.utils.SingletonHolder

internal class StoaFloatViewManager(private val context: Context) : StoaFloatManagerInterface {
    private val TAG = "StoaFloatViewManager"
    var viewTag: String? = null
    private val mStoaViewPos: MutableMap<String, Point> = mutableMapOf()
    private val mLastStoaViewPosInfoMaps: MutableMap<String, LastStoaViewPosInfo> = mutableMapOf()
    private var mStoaFloatManager: StoaFloatManagerInterface? = null

    companion object : SingletonHolder<StoaFloatViewManager, Context>(::StoaFloatViewManager)

    override fun attach(stoaIntent: StoaIntent) {
        mStoaFloatManager?.attach(stoaIntent)
    }

    override fun detach() {
        mStoaFloatManager?.detach()
    }

    override fun detach(tag: String) {
        mStoaFloatManager?.detach(tag)
    }

    override fun detach(stoaFloatView: AbsStoaFloatView) {
        mStoaFloatManager?.detach(stoaFloatView)
    }

    override fun detachAll() {
        mStoaFloatManager?.detachAll()
    }

    override fun getStoaFloatView(activity: Activity, tag: String): IStoaFloatView? {
        return mStoaFloatManager?.getStoaFloatView(activity, tag)
    }

    override fun getStoaFloatViews(activity: Activity, tag: String): Map<String, IStoaFloatView>? {
        return mStoaFloatManager?.getStoaFloatViews(activity, tag)
    }

    override fun onActivityCreate(activity: Activity) {
        mStoaFloatManager?.onActivityCreate(activity)
    }

    override fun onActivityResume(activity: Activity) {
        mStoaFloatManager?.onActivityResume(activity)
    }

    override fun onActivityPause(activity: Activity) {
        mStoaFloatManager?.onActivityPause(activity)
    }

    override fun notifyForeground(activity: Activity) {
        mStoaFloatManager?.notifyForeground(activity)
    }

    override fun notifyBackground(activity: Activity) {
        mStoaFloatManager?.notifyBackground(activity)
    }

    override fun resumeAndAttachStoaViews(activity: Activity) {
        mStoaFloatManager?.resumeAndAttachStoaViews(activity)
    }

    fun saveStoaFloatViewPos(tag: String, marginLeft: Int, marginTop: Int) {
        if (mStoaViewPos[tag] == null) {
            val point = Point(marginLeft, marginTop)
            mStoaViewPos[tag] = point
        } else {
            mStoaViewPos[tag]?.set(marginLeft, marginTop)
        }

    }

    fun getStoaFloatViewPos(tag: String): Point? = mStoaViewPos[tag]

    /**
     * 系统悬浮窗需要调用
     *
     * @param listener
     */
    fun addStoaViewAttachedListener(listener: StoaViewAttachedListener?) {
        if (mStoaFloatManager == null) {
            return
        }
        if (!StoaFloatConstant.IS_NORMAL_FLOAT_MODE && mStoaFloatManager is SysStoaFloatViewManager) {
            (mStoaFloatManager as SysStoaFloatViewManager).addListener(listener)
        }
    }

    /**
     * 系统悬浮窗需要调用
     *
     * @param listener
     */
    fun removeStoaViewAttachedListener(listener: StoaViewAttachedListener?) {
        if (mStoaFloatManager == null) {
            return
        }
        if (!StoaFloatConstant.IS_NORMAL_FLOAT_MODE && mStoaFloatManager is SysStoaFloatViewManager) {
            (mStoaFloatManager as SysStoaFloatViewManager).removeListener(listener)
        }
    }

    fun attachMainIcon(viewTag: String) {
        this.viewTag = viewTag
//        mStoaFloatManager = if (StoaFloatConstant.IS_NORMAL_FLOAT_MODE) {
//            NormalStoaFloatViewManager(context, viewTag)
//        } else {
//            SysStoaFloatViewManager()
//        }
        mStoaFloatManager = lifecycleMap[viewTag]

        val mainIconIntent = StoaIntent(MainIconStoaView(viewTag))
        mainIconIntent.mode = StoaIntent.MODE_SINGLE_INSTANCE
        attach(mainIconIntent)
    }

    interface StoaViewAttachedListener {
        fun onStoaViewAdd(stoaView: AbsStoaFloatView)
    }
}